import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/theme.dart';

import '../../../constants.dart';
import '../../../models/user_account_entity.dart';
import 'logic.dart';

class BalanceRecordPage extends StatelessWidget {
  BalanceRecordPage({Key? key}) : super(key: key);

  final logic = Get.find<BalanceRecordLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: logic.role == 'me' ? const Text('余额变动明细') : const Text('全员余额列表'),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
        bottom: TabBar(
          controller: logic.tabController,
          tabs: logic.tabTitles.map((e) => Tab(text: e)).toList(),
        ),
      ),
      body: _buildListItemBg(),
    );
  }

  Widget _buildListItemBg() {
    return Obx(() {
      if (logic.isLoading.value) {
        return const Center(child: CircularProgressIndicator());
      } else {
        return ListView.separated(
          padding: EdgeInsets.all(kSpaceSmall),
          separatorBuilder: (BuildContext context, int index) => Padding(
            padding: EdgeInsets.symmetric(horizontal: kSpaceSmall),
            child: const Divider(),
          ),
          itemBuilder: _renderRow,
          itemCount: logic.role == 'me'
              ? logic.balanceList.length + 1
              : logic.userList.length + 1,
          controller: logic.scrollController,
        );
      }
    });
  }

  Widget _renderRow(BuildContext context, int index) {
    if (index <
        (logic.role == 'me'
            ? logic.balanceList.length
            : logic.userList.length)) {
      return Ink(
        color: Colors.white,
        child: InkWell(
          onTap: () => logic.role == 'me' ? logic.onClickRecord(index) : null,
          child: Padding(
            padding: EdgeInsets.all(kSpaceSmall),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: logic.role == 'me'
                  ? _itemBuilder(logic.balanceList[index])
                  : _userItemBuilder(logic.userList[index]),
            ),
          ),
        ),
      );
    } else {
      return _getMoreWidget();
    }
  }

  Widget _getMoreWidget() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(kSpaceMedium),
        child: Text(
          logic.bottomTips.value,
          style: TextStyle(fontSize: kFontSizeSmall, color: Colors.grey),
        ),
      ),
    );
  }

  List<Widget> _userItemBuilder(UserAccountFundData item) {
    return [
      SizedBox(height: kSpaceSmall),
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                item.name,
                style: TextStyle(
                  fontSize: kFontSizeMedium,
                  color: AppColor.colorFontBlack,
                ),
              ),
              SizedBox(height: kSpaceTiny),
              Text(
                item.mobile,
                style: TextStyle(
                  fontSize: kFontSizeMini,
                  color: AppColor.colorFontGrey,
                ),
              ),
            ],
          ),
          SizedBox(width: kSpaceMedium),
          Expanded(
            child: Text(
              textAlign: TextAlign.right,
              '余额：${item.amount}',
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
          ),
        ],
      ),
    ];
  }

  List<Widget> _itemBuilder(UserAccountBalanceData item) {
    return [
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Text(
            item.title ?? '',
            style: TextStyle(
              fontSize: kFontSizeMedium,
              color: AppColor.colorFontBlack,
            ),
          ),
          Text(
            item.changeSubtype == 1
                ? '+${item.amount?.replaceAll('¥', '')}'
                : '-${item.amount?.replaceAll('¥', '')}',
            style: TextStyle(
              fontSize: kFontSizeMedium,
              color: AppColor.colorFontBlack,
            ),
          ),
        ],
      ),
      SizedBox(height: kSpaceMini),
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Text(
            item.createdAt == null
                ? ''
                : DateFormat('yyyy.MM.dd HH:mm').format(
                    DateTime.fromMillisecondsSinceEpoch(item.createdAt!)),
            style: TextStyle(
              fontSize: kFontSizeMini,
              color: AppColor.colorFontGrey,
            ),
          ),
          Text(
            item.afterBalance == null
                ? ''
                : '余额 ${item.afterBalance?.replaceAll('¥', '')}',
            style: TextStyle(
              fontSize: kFontSizeMini,
              color: AppColor.colorFontGrey,
            ),
          ),
        ],
      ),
      SizedBox(height: kSpaceSmall),
      Text(
        item.remark ?? '',
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
          fontSize: kFontSizeMini,
          color: AppColor.colorFontGrey,
        ),
      ),
    ];
  }
}
